package com.mp.core.utils.common;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mp.core.utils.MrdNettyResult;
import com.mp.core.utils.RedisUtil;
import com.mp.core.utils.netty.UserChannelRel;
import com.mp.enums.ActionEnum;
import com.mp.enums.ToSendEnum;
import com.mp.netty.NettyHandler;
import com.mp.pojo.VO.WebsiteFlyRecordVO;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * NettyCommon class
 *
 * @author BuK
 * @date 2019/05/30
 */
@Component
public final class NettyCommon {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisCommon redisCommon;



    /**
     * ws成功通知，离线时不保存数据
     *
     * @param channel
     * @param msg
     */
    public void wsSimpleWrite200(Channel channel, ActionEnum actionEnum, String msg, ToSendEnum toSendEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.wsOk(actionEnum, msg, toSendEnum));
    }

    /**
     * ws成功通知，离线时不保存数据
     *
     * @param channel
     * @param data
     */
    public void wsSimpleWrite200(Channel channel, ActionEnum actionEnum, Object data, ToSendEnum toSendEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.wsOk(actionEnum, data, toSendEnum));
    }

    /**
     * ws已经是好友
     *
     * @param channel
     * @param actionEnum
     * @param data
     * @param toSendEnum
     */
    public void wsSimpleWriteError801(Channel channel, ActionEnum actionEnum, Object data, ToSendEnum toSendEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.convertError801(actionEnum, data, toSendEnum));
    }

    /**
     * ws错误提示
     *
     * @param channel
     * @param actionEnum
     * @param msg
     * @param toSendEnum
     */
    public void wsSimpleWriteError802(Channel channel, ActionEnum actionEnum, String msg, ToSendEnum toSendEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.convertError802(actionEnum, msg, toSendEnum));
    }

    /**
     * ws参数错误通知，离线时不保存数据
     *
     * @param channel
     * @param msg
     */
    public void wsSimpleWriteError503(Channel channel, String msg, ActionEnum actionEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.convertError(msg, actionEnum));
    }

    public void wsSimpleWriteErrorBuild(Channel channel, Integer code, String msg, ActionEnum actionEnum, Object data) {
        this.wsSimpleWrite(channel, MrdNettyResult.ErrorBuild(msg, actionEnum, code, data));
    }

    /**
     * ws参数错误通知，离线时不保存数据
     *
     * @param channel
     */
    public void wsServiceError(Channel channel, ActionEnum actionEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.serviceError(actionEnum));
    }

    /**
     * ws参数错误通知，离线时不保存数据
     *
     * @param channel
     */
    public void wsSimpleWriteError503(Channel channel, ActionEnum actionEnum) {
        this.wsSimpleWrite(channel, MrdNettyResult.convertError(actionEnum));
    }


    public Integer wsWrite200(Integer websiteUserId,Object data, ActionEnum action, ToSendEnum toSend) {
        return this.wsWriteOrRedisSave(websiteUserId, data, action, toSend);
    }


    /**
     * ws成功通知，离线时保存数据
     *
     * @param
     * @param data 此数据为data非 MrdNettyResult
     */
    private Integer wsWriteOrRedisSave(Integer websiteUserId, Object data, ActionEnum action, ToSendEnum toSend) {
        Channel userChannel = UserChannelRel.get(websiteUserId);
        if (userChannel != null){
            // 当userChannel不为空的时候，从ChannelGroup去查找对应的channel是否存在
            Channel findChannel = NettyHandler.users.find(userChannel.id());
            if (findChannel != null) {
                // 用户在线
                this.wsSimpleWrite(findChannel, MrdNettyResult.wsOk(action, data, toSend));
                return null;
            }
        }
        return websiteUserId;
    }

    /**
     * ws通知，离线时不保存数据
     *
     * @param
     * @param result
     */
    public void wsSimpleWrite(Channel userChannel, JSONObject result) {
        if (userChannel != null) {
            // 用户在线
            userChannel.writeAndFlush(
                    new TextWebSocketFrame(
                            result.toString()));

            logger.info("channelId: " + userChannel.id().asShortText() + " ws通知:用户在线");
        } else {
            logger.info("channelId: " + userChannel.id().asShortText() + " ws通知:用户离线");
        }
    }

}
